<?php

namespace fastflow;

use fastflow\lib\auth;
use fastflow\lib\lib;

class api
{
    private $lib;

    public function __construct($msgObject = null, $callbackObject = null)
    {
        $this->lib = new lib($msgObject, $callbackObject);
    }

    public function getScope()
    {
        return $this->lib->getScope();
    }

    public function getSignScope()
    {
        return $this->lib->getSignScope();
    }

    public function getDynamicSelectScope()
    {
        return $this->lib->getDynamicSelectScope();
    }

    public function getSelectpageWorkers()
    {
        $param = input('');
        return $this->lib->getSelectpageWorkers($param);
    }


    public function getSelectpageFieldsWithComment()
    {
        $param = input('');
        return $this->lib->getSelectpageFieldsWithComment($param);
    }


    public function getFieldsWithComment($table, $field = '')
    {
        return $this->lib->getFieldsWithComment($table, $field);
    }


    public function getSelectpageOperator()
    {
        $param = input('');
        return $this->lib->getSelectpageOperator($param);
    }

    public function getFlow($flow_id, $isCopy = false)
    {
        $this->lib->isCopy = $isCopy;
        $flows = $this->lib->getFlow(['id' => $flow_id]);
        if ($flows) {
            return $flows[0];
        }
        return false;

    }

    public function getUseableFlowByBill($bill)
    {
        return $this->lib->getFlow(['bill' => $bill, 'status' => 1]);
    }


    public function saveGraph()
    {
        $param = input('');
        return $this->lib->saveGraph($param);
    }


    public function getProcess($process_id)
    {
        $process = $this->lib->getProcess(['id' => $process_id]);
        if ($process) {
            return $process[0];
        }
        return false;
    }


    public function getProcessByFlowId($flow_id)
    {
        $process = $this->lib->getProcess(['flow_id' => $flow_id]);
        if ($process) {
            return $process;
        }
        return false;
    }


    public function getProcessByBill($bill, $bill_id)
    {
        return $this->lib->getProcessByBill($bill, $bill_id);
    }

    public function getThreadById($thread_id)
    {
        return $this->lib->getThreadById($thread_id);
    }


    public function delBill($bill, $bill_id)
    {
        return $this->lib->delBill($bill, $bill_id);

    }


    public function startFlow($param)
    {
        return $this->lib->startFlow($param);
    }


    public function agreeFlow($param)
    {

        $result = $this->lib->agreeFlow($param);
        $result['isFinish'] = $this->lib->isFinish;
        return $result;
    }


    public function signFlow($param)
    {
        return $this->lib->signFlow($param);
    }


    public function backFlow($param)
    {
        return $this->lib->backFlow($param);
    }


    public function getBillAuthInfo($createuser_id, $bill, $bill_id)
    {
        return $this->lib->getBillAuthInfo($createuser_id, $bill, $bill_id);

    }


    public function getRunThreadInfo($bill, $bill_id)
    {
        $this->lib->isCopy = true;
        return $this->lib->getRunThreadInfo($bill, $bill_id);
    }


    public function getBillCanCheckThreadCount($bill)
    {
        return $this->lib->getBillCanCheckThreadCount($bill);
    }


    public function getTableComment($table)
    {
        return $this->lib->getTableComment($table);
    }

    public function getStartDynamicSteps()
    {
        $flow_id = input('flow_id');
        $bill = input('bill');
        $bill_id = input('bill_id');
        $start_nodes = $this->lib->getNodeByType($flow_id, 'start');
        if (!$start_nodes) {
            return ['code' => 0, 'msg' => '未找到该流程开始节点，请检查流程设计是否正确'];
        }
        $start_node = $this->lib->getNodeByType($flow_id, 'start')[0];
        $steps = $this->lib->getDynamicSteps($flow_id, $start_node, $bill, $bill_id);
        $result = [];
        foreach ($steps as $step) {
            $result[] = ['id' => $step['id'], 'name' => $step['data']['name']];
        }
        return ['code' => 1, 'msg' => '获取成功', 'data' => $result];
    }


    public function getCurrentDynamicSteps($flow_id, $thread_id, $is_start = false, $bill = null, $bill_id = null)
    {
        if ($is_start) {
            $this->lib->isCopy = false;
            $current_step = $this->lib->getNodeByType($flow_id, 'start')[0];
            $steps = $this->lib->getDynamicSteps($flow_id, $current_step, $bill, $bill_id);
        } else {
            $this->lib->isCopy = true;
            $thread = $this->lib->getThread(['id' => $thread_id])[0];
            $process = $this->lib->getProcess(['id' => $thread['process_id']])[0];
            $current_step = $this->lib->getNodeById($flow_id, $thread['step_id']);
            $steps = $this->lib->getDynamicSteps($flow_id, $current_step, $process['bill'], $process['bill_id']);
        }
        $result = [];
        foreach ($steps as $step) {
            $result[] = ['id' => $step['id'], 'name' => $step['data']['name']];
        }
        return $result;
    }


    public function getIfHaveDynamicSteps($flow_id, $thread_id, $is_start = false, $bill = null, $bill_id = null)
    {
        if ($is_start) {
            $this->lib->isCopy = false;
            $current_step = $this->lib->getNodeByType($flow_id, 'start')[0];
            $steps = $this->lib->getDynamicSteps($flow_id, $current_step, $bill, $bill_id);
        } else {
            $this->lib->isCopy = true;
            $thread = $this->lib->getThread(['id' => $thread_id])[0];
            $process = $this->lib->getProcess(['id' => $thread['process_id']])[0];

            if ($thread['checkmode'] == 1) {
                $worker_ids = $this->lib->getWorkerIds($process['bill'], $process['bill_id'], $thread);
                $worker_checked_ids = explode(',', $thread['worker_checked_ids']);
                $diff = array_diff($worker_ids, $worker_checked_ids);
                if (count($diff) > 1) {
                    return false;
                }
            }
            $current_step = $this->lib->getNodeById($flow_id, $thread['step_id']);
            $steps = $this->lib->getDynamicSteps($flow_id, $current_step, $process['bill'], $process['bill_id']);
        }
        if (count($steps) > 0) {
            return true;
        }
        return false;
    }


    public function getCanBackPreStep($flow_id, $process_id, $node_id)
    {
        $this->lib->isCopy = true;
        $node = $this->lib->getNodeById($flow_id, $node_id);
        $canBackPreSteps = $this->lib->getCanBackPreStep($flow_id, $process_id, $node);
        $result = [];
        foreach ($canBackPreSteps as $backPreStep) {
            $result[] = ['name' => $backPreStep['data']['name'], 'id' => $backPreStep['id']];
        }
        if (count($result) == 0) {
            $threads = $this->lib->getThread(['process_id' => $process_id, 'step_id' => $node_id, 'status' => 1]);
            if ($threads) {
                $prethread_id = $threads[0]['prethread_id'];
                $prethreads = $this->lib->getThread(['id' => $prethread_id]);
                if ($prethreads) {
                    $pre_step_id = $prethreads[0]['step_id'];
                    $pre_step = $this->lib->getNodeById($flow_id, $pre_step_id);
                }
                $result[] = ['name' => $pre_step['data']['name'], 'id' => $pre_step['id']];
            }
        }
        return $result;
    }


    public function getFlowProgress($bill, $bill_id)
    {
        $this->lib->isCopy = true;
        return $this->lib->getFlowProgress($bill, $bill_id);
    }


    public function terminateProcess($id)
    {
        return $this->lib->terminateProcess($id);
    }


    public function getBillRow($bill, $bill_id)
    {
        return $this->lib->getBillRow($bill, $bill_id);
    }

    public function getBillRows($bill, $where = ''){
        return $this->lib->getBillRows($bill, $where);
    }

    public function getBillRowsCount($bill, $where = '')
    {
        return $this->lib->getBillRowsCount($bill, $where);
    }


    public function getRunningBillRows($bill)
    {
        return $this->lib->getBillRows($bill, ['status' => 1]);
    }

    public function buidMessageParams($params)
    {
        $this->lib->isCopy = true;
        return $this->lib->buidMessageParams($params);
    }

    public function getThreadLog($bill, $bill_id)
    {
        return $this->lib->getThreadLog($bill, $bill_id);
    }

    public function getLogByIds($bill, $bill_id, $log_ids)
    {
        return $this->lib->getLogByIds($bill, $bill_id, $log_ids);
    }


    public function getCarbonRange()
    {
        return $this->lib->getCarbonRange();
    }

    public function getCarbonScope()
    {
        return $this->lib->getCarbonScope();
    }

    public function checkAgency($bill, $bill_id, $scope, $workers)
    {
        return $this->lib->checkAgency($bill, $bill_id, $scope, $workers);
    }

    public function getAllSteps($flow_id)
    {
        return $this->lib->getAllStep($flow_id);
    }

    public function checkThreadAuth($thread)
    {
        return $this->lib->checkThreadAuth($thread);
    }

    public function getAllNodes($flow_id)
    {
        return $this->lib->getAllNodes($flow_id);
    }

    public function getWorkerNameStr($worker_ids, $scope)
    {
        return $this->lib->getWorkerNameStr($worker_ids, $scope);
    }

    public function getNodeById($flow_id, $id)
    {
        return $this->lib->getNodeById($flow_id, $id);
    }

    public function getAuthRow($param)
    {
        return $this->lib->getAuthRow($param);
    }

    public function setAuthRow($param)
    {
        return $this->lib->setAuthRow($param);
    }

    public function addAuthRow($param)
    {
        return $this->lib->addAuthRow($param);
    }

    public function checkStartAuth($flow_id)
    {
        return (new auth())->checkStartAuth($flow_id);
    }

    public function getCanEditFields($bill, $bill_id, $thread_id = null)
    {
        return (new auth())->getCanEditFields($bill, $bill_id, $thread_id);
    }

    public function getBillFields($bill)
    {
        return (new auth())->getBillFields($bill);
    }

    public function test()
    {
        return $this->lib->test();
    }


}